package com.channel.modules.security.service.dto;

import com.alibaba.fastjson.annotation.JSONField;
import com.channel.modules.system.service.dto.UserLoginDto;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

public class JwtUserDto implements UserDetails {

    private final UserLoginDto user;
    private final List<Long> dataScopes;
    private final List<AuthorityDto> authorities;

    public JwtUserDto(UserLoginDto user, List<Long> dataScopes, List<AuthorityDto> authorities) {
        this.user = user;
        this.dataScopes = dataScopes;
        this.authorities = authorities;
    }

    public UserLoginDto getUser() {
        return user;
    }

    public List<Long> getDataScopes() {
        return dataScopes;
    }

    public Set<String> getRoles(){
        return authorities.stream().map(AuthorityDto::getAuthority).collect(Collectors.toSet());
    }


    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return Collections.emptyList();
    }

    @Override
    @JSONField(serialize = false)
    public String getPassword() {
        return user.getPassword();
    }

    @Override
    @JSONField(serialize = false)
    public String getUsername() {
        return user.getUsername();
    }

    @Override
    @JSONField(serialize = false)
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    @JSONField(serialize = false)
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    @JSONField(serialize = false)
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    @JSONField(serialize = false)
    public boolean isEnabled() {
        return user.getEnabled();
    }
}
